import axios from 'axios'
import qs from 'qs'
import ip from './ip.js'
import env from './_env.js'

var exp = {}

if(env.mode == 1){
    let axiosIns = axios.create({});
    const _axios = {}


    // if (process.env.NODE_ENV == 'dev') {
    axiosIns.defaults.baseURL = 'http://'+ ip;
    // } else if (process.env.NODE_ENV == 'debug') {
        // axiosIns.defaults.baseURL = 'http://chat.ha.aoxing.aoxingtec.cn';
    // } else if (process.env.NODE_ENV == 'production') {
    //     axiosIns.defaults.baseURL = 'http://www.aoxingtec.cn';
    // }

    // 跨域带cookie
    // Access-Control-Allow-Credentials: true 
    axiosIns.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
    axiosIns.defaults.headers.get['X-Requested-With'] = 'XMLHttpRequest';
    axiosIns.defaults.headers['Content-Type']="application/x-www-form-urlencoded"
    axiosIns.defaults.responseType = 'json';
    axiosIns.defaults.transformRequest = [function (data) {
        //数据序列化
        return qs.stringify(data);
    }
    ];
    axiosIns.defaults.validateStatus = function (status) {
        return true;
    };
    axiosIns.interceptors.request.use(function (config) {
        //配置config
        config.headers.Accept = 'application/json';
        // config.headers.System = 'vue';
        // let token = Vue.localStorage.get('token');
        // if(token){
        //     config.headers.Token = token;
        // }
        return config;
    });
    axiosIns.interceptors.response.use(function (response) {
        let data = response.data;
        let status = response.status;
        if (status === 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    });

let ajaxMethod = ['get', 'post'];
let api = {};

ajaxMethod.forEach((method)=> {
    //数组取值的两种方式
    api[method] = function (uri, data, config) {
        return new Promise(function (resolve, reject) {
            axiosIns[method](uri, data, config).then((response)=> {
                /*根据后台数据进行处理
                 *1 code===200   正常数据+错误数据     code!==200   网络异常等
                 *2 code===200   正常数据     code!==200   错误数据+网络异常等
                 * 这里使用的是第一种方式
                 * ......
                 */
                if (response.data.StatusCode) {
                    //toast封装：  参考ele-mint-ui
                    Toast({
                        message: response.data.Message,
                        position: 'top',
                        duration: 2000
                    });
                    if (response.data.Message === '未登录') {
                        Toast({
                            message: response.data.Message,
                            position: '',
                            duration: 2000
                        });
                        //使用vue实例做出对应行为  change state or router
                        //instance.$store.commit('isLoginShow',true);
                    }
                } else {
                    resolve(response);
                }
            }).catch((response)=> {
                //reject response
                //alert('xiuxiu，限你10分钟到我面前来,不然...');
            })
        })
    }
});

exp = api

}else{

    axios.defaults.baseURL = "http://127.0.0.1:4000"
    axios.defaults.headers.post['Content-Type']="application/x-www-form-urlencoded"
    axios.defaults.headers.get['Content-Type']="application/x-www-form-urlencoded"
    axios.defaults.responseType = 'json';
    axios.defaults.transformRequest = [function (data) {
        //数据序列化
        return qs.stringify(data);
    }
    ];

    exp = axios

}

export default exp